package com.ruoyi.yx.wxpay;

import com.ruoyi.common.utils.uuid.UUID;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.security.core.parameters.P;

public class JsapiServiceExtensionExample {
  /** 商户号 */
  public static String merchantId = "1672725034";
  /** 商户号 */
  public static String wxAppId = "wxb3188d1a96bf9733";


  /** 商户API私钥路径 */
  public static String privateKeyPath = "/Users/wangzhen/Documents/workspace/self/yx_open_mdm/1672725034_20240402_cert/apiclient_key.pem";
  /** 商户证书序列号 */
  public static String merchantSerialNumber = "66FAF3FB222FC09E8B2816CB92C7CA91C9AE35F2";
  /** 商户APIV3密钥 */
  public static String apiV3Key = "003761ab541a4c949234d8c1d979b077";

  /** 支付回调地址 */
  public static String notify_url = "https://openmdm.yxmdm.com/api/pay/wx/notify";

  public static JsapiServiceExtension service;

  public static void main(String[] args) {
    // 初始化商户配置
    Config config =
        new RSAAutoCertificateConfig.Builder()
            .merchantId(merchantId)
            // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
            .privateKeyFromPath(privateKeyPath)
            .merchantSerialNumber(merchantSerialNumber)
            .apiV3Key(apiV3Key)
            .build();
    // 初始化服务
    service =
        new JsapiServiceExtension.Builder()
            .config(config)
            .signType("RSA") // 不填默认为RSA
            .build();
    try {
      // ... 调用接口
      String out_trade_no = UUID.randomUUID().toString().replace("-","");
      Integer total  = 1;
      String description = "测试支付";
      String openId = "ovMWZ6_wVfjR2A5gkkKoIHI18ejU";
      PrepayWithRequestPaymentResponse response = prepayWithRequestPayment(out_trade_no,total,description,openId);
      System.out.println(response);
    } catch (HttpException e) { // 发送HTTP请求失败
      // 调用e.getHttpRequest()获取请求打印日志或上报监控，更多方法见HttpException定义
      e.printStackTrace();
    } catch (ServiceException e) { // 服务返回状态小于200或大于等于300，例如500
      // 调用e.getResponseBody()获取返回体打印日志或上报监控，更多方法见ServiceException定义
      e.printStackTrace();
    } catch (MalformedMessageException e) { // 服务返回成功，返回体类型不合法，或者解析返回体失败
      // 调用e.getMessage()获取信息打印日志或上报监控，更多方法见MalformedMessageException定义
      e.printStackTrace();
    }
  }
  /** 关闭订单 */
  public static void closeOrder() {

    CloseOrderRequest request = new CloseOrderRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    service.closeOrder(request);
  }
  /** JSAPI支付下单，并返回JSAPI调起支付数据 */
  public static PrepayWithRequestPaymentResponse prepayWithRequestPayment(String out_trade_no,Integer total,String description,String openId) {
    PrepayRequest request = new PrepayRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    request.setMchid(merchantId);
    request.setOutTradeNo(out_trade_no);
    request.setAppid(wxAppId);
    request.setDescription(description);
    request.setNotifyUrl(notify_url);
    Amount amount = new Amount();

    amount.setCurrency("CNY");
    amount.setTotal(total);
    request.setAmount(amount);

    Payer payer = new Payer();
    payer.setOpenid(openId);
    request.setPayer(payer);

    // 调用接口
    return service.prepayWithRequestPayment(request);
  }
  /** 微信支付订单号查询订单 */
  public static Transaction queryOrderById() {

    QueryOrderByIdRequest request = new QueryOrderByIdRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    return service.queryOrderById(request);
  }
  /** 商户订单号查询订单 */
  public static Transaction queryOrderByOutTradeNo() {

    QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    return service.queryOrderByOutTradeNo(request);
  }
}
